package com.xyf.sys.controller;

import com.xyf.sys.config.MiniprogramConfig;
import com.xyf.sys.domain.ChannelConfig;
import com.xyf.sys.domain.Dept;
import com.xyf.sys.domain.Role;
import com.xyf.sys.domain.User;
import com.xyf.sys.service.IDeptService;
import com.xyf.sys.service.IUserRoleService;
import com.xyf.sys.service.IUserService;
import com.xyf.sys.util.R;
import com.xyf.sys.vo.UserListRequestVo;
import com.xyf.sys.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author R-H
 * @Date 2024/12/41425
 * @Description
 */
@RestController
@RequestMapping("/dept")
@Slf4j
@Api(tags = "部门")
public class DeptController {

    @Autowired
    private IDeptService deptService;
    @Autowired
    private IUserService userService;
    @Autowired
    @Qualifier(value = "clientConfig")
    private MiniprogramConfig.MiniConfig clientConfig;
    @Autowired
    @Qualifier(value = "manageConfig")
    private MiniprogramConfig.MiniConfig manageConfig;
    @Autowired
    private IUserRoleService roleService;

    @ApiOperation("查询部门列表")
    @PostMapping("/list")
    public R list(@RequestBody Map<String, String> body,HttpServletRequest request){
        return R.ok(deptService.getList(body.get("keyword"), request.getParameter("openid")));
    }

    @ApiOperation("新增部门")
    @PostMapping("/add")
    public R add(@RequestBody Dept dept){
        return R.ok(deptService.add(dept));
    }

//    @ApiOperation("更新部门")
//    @PostMapping("/update")
//    public R update(@RequestBody Dept dept){
//        return R.ok(deptService.update(dept));
//    }

    @ApiOperation("更新部门")
    @PostMapping("/edit")
    public R edit(@RequestBody Dept dept, HttpServletRequest request){
        deptService.edit(dept);
        return R.ok(deptService.queryInfo(dept.getDeptId(), dept.getUserId()));
    }

    @ApiOperation("删除")
    @PostMapping("/del")
    public R del(@RequestBody Dept dept){
        return R.ok(deptService.remove(dept.getDeptId(), dept.getUserId()));
    }


    @ApiOperation("查询部门用户列表")
    @PostMapping("/userList")
    public R userList(@RequestBody UserListRequestVo requestVo, HttpServletRequest request){
        return R.ok(deptService.getUserList(requestVo));
    }

    @ApiOperation("移除")
    @PostMapping("/removeUser")
    public R removeUser(@RequestBody User user, HttpServletRequest request){
        return R.ok(deptService.removeFollowUser(user.getDeptId(), user.getUserId() , request.getHeader("OPENID")));
    }

    @ApiOperation("查询部门")
    @GetMapping("/info")
    public R info(@RequestParam Long deptId, HttpServletRequest request){
        return R.ok(deptService.queryInfo(deptId, request.getHeader("openid")));
    }

    @ApiOperation("")
    @PostMapping("/search")
    public R search(@RequestBody Map<String, String> body, HttpServletRequest request){
        return R.ok(deptService.search(body.get("keyword")));
    }

    @ApiOperation("用户-视频号配置")
    @PostMapping("/channelConfig")
    public Object channelConfig(@RequestBody ChannelConfig config, HttpServletRequest request) {
        userService.updateChannelConfig(config,  request.getHeader("OPENID"));
        UserVo userInfo = userService.getInfo(request.getHeader("OPENID"), request.getHeader("APPID").equals(clientConfig.getAppid()));
        return R.ok(userInfo);
    }
}
